class AddEmployeePosition < Mutations::Command
  include Wisper::Publisher

  required do
    integer :employee_id
    integer :position_id
    integer :organization_id
    date    :induction_time
  end
  
  def execute
    unless employee = Employee.find_by_id(employee_id)
      add_error(:employee_id, :employee_not_exist, '员工不存在')
      return
    end

    unless position = Position.find_by_id(position_id)
      add_error(:position_id, :position_not_exist, '岗位不存在')
      return
    end

    unless organization = Organization.find_by_id(organization_id)
      add_error(:organization_id, :organization_not_exist, '组织不存在')
      return
    end

    employee_position = Employee::Position.new inputs
    if employee_position.save
      return employee_position
    else
      employee_position.errors.each do |key, value|
        add_error(key, key, value)
      end
    end
  end
  
end